package net.kem.android.positioner.responsehandler;

import java.util.LinkedList;

import net.kem.android.positioner.POI;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;

/**
 *
 <message>
	<phoneNumber>0507362270</phoneNumber> <!-- for debug purposes only -->
	<status>0</status>         <!-- Integer. 0 if get all favorites operation was successful. -->
	<descr>bla.bla</descr>     <!-- Optional string. Contains error description if status != 0 -->
	<favorites>
		<favorite>
			<idFavorite>1</idFavorite> 
			<idType>1</idType> 
			<name>Work</name> 
			<lat>35.334</lat> 
			<long>45.567</long>
		</favorite>
		<favorite>
			<idFavorite>2</idFavorite>
			<idType>1</idType> 
			<name>Home</name> 
			<lat>45.567</lat> 
			<long>35.334</long>
		</favorite>
 	</favorites> 
 </message>
 * @author Zhenya
 *
 */
public class GetAllPOIsResponseHandler extends ResponseHandler {
//	class POI {
//		private int id;
//		private int type;
//		private String name;
//		private float lat;
//		private float lon;
//		private int accuracy; // Reserved for future protocol extensions.
//		private int radius; // Reserved for future protocol extensions.
//		private String notes; // Reserved for future protocol extensions.
//	}

	private LinkedList<POI> pois;
	private POI currentPOI;

	@Override
	public void startDocument() throws SAXException {
		pois = new LinkedList<POI>();
	}

	@Override
	public void startElement(String uri, String localName, String qName, Attributes atts) throws SAXException {
		super.startElement(uri, localName, qName, atts);
		if(localName.equals("favorite")) {
			currentPOI = new POI();
		}
	}

	@Override
	public void endElement(String uri, String localName, String qName) throws SAXException {
		if(localName.equals("favorite")) {
			pois.add(currentPOI);
		} else if(localName.equals("idFavorite")) {
			currentPOI.setId(Integer.parseInt(currentValue));
		} else if(localName.equals("idType")) {
			currentPOI.setType(Integer.parseInt(currentValue));
		} else if(localName.equals("name")) {
			currentPOI.setName(currentValue);
		} else if(localName.equals("lat")) {
			currentPOI.getCoordinates().setLatitude(Double.parseDouble(currentValue));
		} else if(localName.equals("long")) {
			currentPOI.getCoordinates().setLongitude(Double.parseDouble(currentValue));
		}
		// Reserved for future protocol extensions.
//		else if(localName.equals("alt")) {
//			currentPOI.getCoordinates().setAltitude(Double.parseDouble(currentValue));
//		} else if(localName.equals("accuracy")) {
//			currentPOI.getCoordinates().setAccuracy(Float.parseFloat(currentValue));
//		} else if(localName.equals("radius")) {
//			currentPOI.setRadius(Integer.parseInt(currentValue));
//		} else if(localName.equals("notes")) {
//			currentPOI.setNotes(currentValue);
//		}
		else {
			super.endElement(uri, localName, qName);
		}
	}

	public LinkedList<POI> getPois() {
		return pois;
	}
}